package snapex.core.model;

import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import snapex.core.Operation;
import snapex.core.Status;

@Setter
@Getter
@NoArgsConstructor
@JsonInclude(Include.NON_NULL)
public class Result<T> {
	private String id;
//	private String message;
	private Operation operation;
//	private OperationStatus status;
//    private T item;
    private Map<String,String> globalErrors;
    private Map<String,String> fieldErrors;
    
    private Integer code;
    private String msg;
    private T data;
   
	public Result(Status status) {
		this.setCode(status.getCode());
		this.setMsg(status.getMsg());
	}

	public static Result success(Operation operation) {
		return Result.success().operation(operation);
	}

	public static Result success() {
		return new Result(Status.Ok);
	}

	public static Result failure(Operation operation) {
		return Result.failure().operation(operation);
	}

	public static Result failure() {		
		return Result.failure(Status.InternalError);
	}

	public static Result failure(Status status) {		
		return new Result(status);
	}
	
	public Result code(Integer code) {
		this.setCode(code);
		return this;
	}

	public Result msg(String msg) {
		this.setMsg(msg);
		return this;
	}
	
	public Result id(String id) {
		this.id = id;
		return this;
	}
	
	public Result operation(Operation operation) {
		this.setOperation(operation);
		return this;
	}
	
//	public Result item(T item) {
//		this.setItem(item);
//		return this;
//	}
	
	public Result data(T data) {
		this.data = data;
		return this;
	}
	
	public Result fieldErrors(Map<String,String> errors) {
		this.fieldErrors = errors;
		return this;
	}
	
	public Result globalErrors(Map<String,String> errors) {
		this.globalErrors = errors;
		return this;
	}
}
